local DIY = require "packages.diy_utility.diy_utility"
local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill {
  name = "lb_zero__zhanyuan",
  tags = { LB_U.LeaderSkill }
}

Fk:loadTranslationTable {
  ["lb_zero__zhanyuan"] = "斩渊",
  [":lb_zero__zhanyuan"] = "<b><a href='lb_leader_skill'><font color='red'>领袖技</font></a></b>，每回合结束时，若本回合有“对空六课”角色的伤害牌不因使用进入弃牌堆，你可将其中不同花色的各一张牌分配给同势力的任意名角色，然后视为使用剩余的一张牌。",

  ["#lb_zero__zhanyuan"] = "斩渊：你可以将其中不同花色的各一张牌分配给同势力的任意名角色，然后视为使用剩余的一张牌",
  ["#lb_zero__zhanyuan-give"] = "斩渊：请将这些牌分配给“对空六课”角色",

  ["$lb_zero__zhanyuan1"] = "不堪一击。",
  ["$lb_zero__zhanyuan2"] = "不留余地。",
  ["$lb_zero__zhanyuan3"] = "乘胜追击。",
}

Fk:addPoxiMethod {
  name = skel.name,
  card_filter = function(to_select, selected, data, extra_data)
    local suits = {}
    for _, id in ipairs(selected) do
      table.insert(suits, Fk:getCardById(id).suit)
    end
    return #selected < #extra_data.default and not table.contains(suits, Fk:getCardById(to_select).suit)
  end,
  feasible = function(selected, data, extra_data)
    return #selected == #extra_data.default
  end,
  prompt = "#lb_zero__zhanyuan",
}

skel:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local room = player.room
      local logic = room.logic
      local all_cards = {}
      for _, id in ipairs(DIY.getCenterCards(room)) do
        if Fk:getCardById(id).is_damage_card then
          logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
            local move = e.data
            for i = #move, 1, -1 do
              local moveInfo = move[i].moveInfo
              if move[i].from and LB_U.CompareKingdomWith(move[i].from, "lb_zero_duikongliuke") then
                for j = #moveInfo, 1, -1 do
                  if moveInfo[j].cardId == id then
                    if move[i].moveReason ~= fk.ReasonUse then -- 只找最后一次移动
                      table.insert(all_cards, id)
                    end
                    return true
                  end
                end
              end
            end
          end, nil, Player.HistoryTurn)
        end
      end
      if #all_cards > 0 then
        event:setCostData(self, { all_cards = all_cards })
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local all_cards = event:getCostData(self).all_cards
    if all_cards and #all_cards > 0 then
      local default = {} -- 复制一下
      for _, suit in ipairs({
        Card.Spade,
        Card.Club,
        Card.Heart,
        Card.Diamond,
      }) do
        local ids = table.filter(all_cards, function(id)
          return Fk:getCardById(id).suit == suit
        end)
        if #ids > 0 then
          table.insert(default, ids[1])
        end
      end
      local cards = room:askToPoxi(player, {
        poxi_type = skel.name,
        cancelable = true,
        data = {
          { "pile_discard", all_cards },
        },
        extra_data = { default = default }
      })
      if #cards > 0 then
        event:setCostData(self, { all_cards = all_cards, cards = cards })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    local all_cards = event:getCostData(self).all_cards
    local players = table.filter(room.alive_players, function(to)
      return LB_U.CompareKingdomWith(to, "lb_zero_duikongliuke")
    end)
    while #players > 0 and player:isAlive() and #cards > 0 do
      local tos = { players[1] }
      local ids = table.simpleClone(cards)
      if #players > 1 then
        tos, ids = room:askToChooseCardsAndPlayers(player, {
          min_num = 1,
          max_num = 1,
          min_card_num = 1,
          max_card_num = #cards,
          targets = players,
          pattern = tostring(Exppattern { id = cards }),
          skill_name = skel.name,
          prompt = "#lb_zero__zhanyuan-give",
          cancelable = false,
          expand_pile = cards,
        })
      end
      if #tos > 0 and #ids > 0 then
        for _, id in ipairs(ids) do
          table.removeOne(cards, id)
          table.removeOne(all_cards, id)
        end
        room:moveCardTo(ids, Card.PlayerHand, tos[1], fk.ReasonJustMove, skel.name, nil, true, player)
        players = table.filter(room.alive_players, function(to)
          return LB_U.CompareKingdomWith(to, "lb_zero_duikongliuke")
        end)
        cards = table.filter(cards, function(id)
          return room:getCardArea(id) == Card.DiscardPile
        end)
        all_cards = table.filter(all_cards, function(id)
          return room:getCardArea(id) == Card.DiscardPile
        end)
      else
        break
      end
    end
    if player:isAlive() and #all_cards > 0 then
      local names = {}
      for _, id in ipairs(all_cards) do
        table.insertIfNeed(names, Fk:getCardById(id).name)
      end
      room:askToUseVirtualCard(player, {
        name = names,
        skill_name = skel.name,
        cancelable = false,
      })
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
end)


return skel
